//
//  SecondViewController.m
//  UI10_homework通讯录UI
//
//  Created by dllo on 16/6/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "LTView.h"
#import "ViewController.h"


@interface SecondViewController ()<UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property(nonatomic, retain)NSArray *enterInArray;
@property(nonatomic, retain)NSArray *contentsArray;



@property(nonatomic, retain)LTView *nameLT;
@property(nonatomic, retain)LTView *numberLT;
@property(nonatomic, retain)LTView *qqLT;


@property(nonatomic, retain)UIImageView *imageView;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.name;
    self.enterInArray = [NSArray array];
    self.contentsArray = [NSArray array];
    
    
    
    [self creatContactView];
    
}




-(void)creatContactView{
    self.contentsArray = @[@"姓名", @"电话", @"QQ"];
    self.enterInArray = @[self.name, self.number, self.qq];
    
    //创建头像
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) / 3, 84, CGRectGetWidth(self.view.frame) / 3, CGRectGetHeight(self.view.frame) / 5)];
    
    self.imageView.userInteractionEnabled = YES;////// !!!!!!remember!!!!!!!
    
    self.imageView.image = self.headerImage;
    self.imageView.backgroundColor = [UIColor yellowColor];
    
    [self creatGesture];
    [self.view addSubview:_imageView];
    
    // 添加一个手势
    
    
//    // 创建Label;
//    for (int i = 0; i < self.contentsArray.count; i++) {
//        
//        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, i * CGRectGetHeight(self.view.bounds) / 12 + 300, 100, 60)];
//        label.textAlignment = 1;
//        label.backgroundColor = [UIColor yellowColor];
//        label.text = self.contentsArray[i];
//        [self.view addSubview:label];
//        [label release];
//        
//    }
//    
//    // 创建textField // 弄一个LTView好了!
//    for (int i = 0; i < self.enterInArray.count; i ++) {
//        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(190, i * CGRectGetHeight(self.view.bounds) / 12 + 300, 140, 60)];
//        textField.textAlignment = 1;
//        textField.backgroundColor = [UIColor yellowColor];
//        textField.text = self.enterInArray[i];
//        textField.delegate = self;
//        [self.view addSubview:textField];
//        [textField release];
//    }

    // 创建LTView
    self.nameLT = [[LTView alloc]initWithFrame:CGRectMake(60, 300, CGRectGetWidth(self.view.bounds) - 120, 40) withLabelText:@"姓名" withFieldText:self.name];
    
    self.numberLT = [[LTView alloc]initWithFrame:CGRectMake(60, 400, CGRectGetWidth(self.nameLT.frame), CGRectGetHeight(self.nameLT.frame)) withLabelText:@"电话" withFieldText:self.number];
    
    self.qqLT= [[LTView alloc]initWithFrame:CGRectMake(60, 500, CGRectGetWidth(self.numberLT.frame), CGRectGetHeight(self.numberLT.frame)) withLabelText:@"QQ" withFieldText:self.qq];
    [self.view addSubview:_nameLT];
    [self.view addSubview:_numberLT];
    [self.view addSubview:_qqLT];
    [_nameLT release];
    [_numberLT release];
    [_qqLT release];
    
}

-(void)setContentsArray:(NSArray *)contentsArray{
    
    if (_contentsArray != contentsArray) {
        [_contentsArray release];
        _contentsArray = [contentsArray retain];
    }
    
//    [self creatSubs];
    
    
}

-(void)setEnterInArray:(NSArray *)enterInArray{
    
    if (_enterInArray != enterInArray) {
        [_enterInArray release];
        _enterInArray = [enterInArray retain];
    }
//    [self creatSubs];
    
}




-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    return YES;
}



-(void)viewDidDisappear:(BOOL)animated{
    
   
        [self.delegate sentNameText:self.nameLT.textField.text withNumber:self.numberLT.textField.text withQQ:self.qqLT.textField.text withNewImage:self.headerImage];
   

}


-(void)creatGesture{
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    [self.imageView addGestureRecognizer:tap];
}
-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    NSLog(@"我去你大爷");
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    
    //    图片从哪里获取
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    // 设置成允许用户编辑
    picker.allowsEditing = YES;
    
    picker.delegate = self;
    
    // 从一个controller跳到另一个controller的方法 模态推出
    [self presentViewController:picker animated:YES completion:^{
        
    }];

    
    
    
    
}


//实现协议里的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"%@", info);
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    // UIImagePickerControllerEditedImage 如果这里写这句 就说明选择上的是编辑后的图片.
    [self.imageView setImage:image];
    
    self.headerImage = image;
    
    // 退出运行的相册
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
